package com.lansi.managers.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import com.lansi.managers.bean.Manager;
import com.lansi.managers.mapper.ManagerMapper;
import com.lansi.service.ex.TelAlreadyExistException;
import com.lansi.service.ex.TelFormatException;
import com.lansi.service.ex.UserNotFoundException;
import com.lansi.service.ex.UsermailFormatException;
import com.lansi.service.ex.UsernameAlreadyExistException;

@Service
public class ManagerService implements IManagersService{
	@Resource
	private ManagerMapper  managerMapper;
	private String salt = "蓝思?";
	
	//添加蓝思用户
	public void register(Manager manager) {
		Manager manager1 = managerMapper.selectManagerByUsername(manager.getUsername());//检测数据用户名是否存在
		Manager manager2 = managerMapper.selectManagerByTel(manager.getTel());		
		String tel1 = manager.getTel();
		String usermail1 = manager.getUsermail();
		if(!tel1.matches("^1[0-9]{10}$")) {
			throw new TelFormatException("手机格式错误");
		}
		else if(!usermail1.contains("@") || !usermail1.contains(".")) {
			throw new UsermailFormatException("邮箱格式错误");
		}
		else if(tel1.matches("^1[0-9]{10}$") && usermail1.contains("@")  && usermail1.contains(".")){
			if(manager1==null && manager2==null) {
				//明文密码
				String pwd = manager.getUserkey();
				//生成密文
				String pwdMd5 = DigestUtils.md5Hex(pwd+salt);
				//manager对象中设置密文
				manager.setUserkey(pwdMd5);
				managerMapper.insertManager(manager);
			}
			else if(manager1!=null){
				throw new UsernameAlreadyExistException("用户名已存在");
				}
			else if(manager2!=null){
				throw new TelAlreadyExistException("手机号已存在") ;
				}
			
		}
			
	}
	//根据id删除用户信息-可以批量删除
	public void delManager(Integer[] ids) {
		managerMapper.deleteManager(ids);
		
	}
	//单个删除用户，根据id
	public void delManagerById(Integer id) {
		managerMapper.deleteManagerById(id);
		
	}
	//修改蓝思用户信息
	public void updateManager(Integer id, String username, String userkey, String tel, String usermail, Date ctime,
			String prio) {
		Manager manager = new Manager();
		manager.setId(id);
		//manager.setUsername(username);
		manager.setUserkey(userkey);
		//manager.setTel(tel);
		manager.setUsermail(usermail);
		manager.setCtime(ctime);
		manager.setPrio(prio);
		
		Manager manager1 = managerMapper.selectManagerById(id);//查看数据库是否存在此用户
		if(manager1==null) {
			//2.manager1==null抛出UserNotFoundException
			throw new UserNotFoundException("用户不存在");
		}else {
			//查询数据库用户名是否存在
			Manager manager2 = managerMapper.selectManagerByUsername(username);
			//查询数据库手机是否存在
			Manager manager3 = managerMapper.selectManagerByTel(tel);
			if(manager2!=null  ) {//用户名
				if(manager2.getUsername().equals(manager1.getUsername())) {
					
				}else {
					throw new UsernameAlreadyExistException("用户名已存在");
					}
			}
			if(manager3!=null) {//手机
				if(manager3.getUsername().equals(manager1.getUsername())) {
					
				}else {
					throw new UsernameAlreadyExistException("手机已存在");
					}
			}
			if(manager2==null && manager3==null ){
				manager.setUsername(username);
				manager.setTel(tel);
			}
		}
		managerMapper.updateManager(manager);
		
	}
	//根据id查询
	public Manager getManagerById(Integer id) {
		
		return managerMapper.selectManagerById(id);
	}
	//根据用户名查询
	public Manager getManagerByUsername(String username) {
		
		return managerMapper.selectManagerByUsername(username);
	}
	//条件查询结果数量
	public Integer getManagercount(String username, String tel) {
		
		return managerMapper.selectManagercount(username, tel);
	}
	//条件查询结果
	public List<Manager> getManager(String username, String tel, Integer offset, Integer count) {
		
		return managerMapper.selectManager(username, tel, offset, count);
	}
	//根据手机查询
	public Manager getManagerByTel(String tel) {
		
		return managerMapper.selectManagerByTel(tel);
	}

}
